<template>
  <div class="volunteer-profile">
    <div class="page-header">
      <h2>志愿者档案</h2>
      <el-button type="primary" @click="handleEdit" v-if="!isEditing">编辑</el-button>
      <div v-else>
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </div>
    </div>

    <el-card>
      <el-form :model="profileForm" label-width="100px" :disabled="!isEditing">
        <el-form-item label="志愿者编号">
          <el-input v-model="profileForm.volunteerId" disabled />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="profileForm.name" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="profileForm.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="profileForm.className" />
        </el-form-item>
        <el-form-item label="二级学院">
          <el-input v-model="profileForm.department" />
        </el-form-item>
        <el-form-item label="加入时间">
          <el-date-picker v-model="profileForm.joinDate" disabled />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="profileForm.phone" />
        </el-form-item>
        <el-form-item label="电子邮箱">
          <el-input v-model="profileForm.email" />
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="activity-records" style="margin-top: 20px">
      <template #header>
        <div class="card-header">
          <span>活动记录</span>
        </div>
      </template>
      
      <!-- 添加查询表单 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="活动标题">
          <el-input v-model="searchForm.title" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
            v-model="searchForm.timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="filteredActivityRecords">
        <el-table-column prop="activityId" label="活动编号" width="100" />
        <el-table-column prop="title" label="活动标题" />
        <el-table-column prop="startTime" label="开始时间" width="180" />
        <el-table-column prop="endTime" label="结束时间" width="180" />
        <el-table-column prop="role" label="角色" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.role === '负责人' ? 'success' : 'info'">
              {{ scope.row.role }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="考核分数" width="100" />
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getUserProfile, updateUserProfile } from '@/api/user'

export default {
  data() {
    return {
      isEditing: false,
      profileForm: {
        volunteerId: '',
        name: '',
        gender: '男',
        className: '',
        department: '',
        joinDate: '',
        phone: '',
        email: ''
      },
      activityRecords: [],
      searchForm: {
        title: '',
        timeRange: []
      }
    }
  },
  created() {
    this.fetchProfile()
  },
  methods: {
    async fetchProfile() {
      try {
        const userId = this.$store.state.user.id
        const data = await getUserProfile(userId)
        this.profileForm = data.profile
        this.activityRecords = data.activityRecords
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleSave() {
      try {
        const userId = this.$store.state.user.id
        await updateUserProfile(userId, this.profileForm)
        this.isEditing = false
        this.$message.success('保存成功')
        this.fetchProfile()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}

.activity-records {
  margin-top: 20px;
}
</style> 